# 测试后端 Swagger API
# 服务地址: http://localhost:8080/doc
# Swagger UI: http://localhost:8080/doc/swagger-ui/index.html#/

$baseUrl = "http://localhost:8080/doc/api/v1"
$token = $null

Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "  文档管理系统 API 测试" -ForegroundColor Cyan
Write-Host "  Base URL: $baseUrl" -ForegroundColor Cyan
Write-Host "  Swagger: http://localhost:8080/doc/swagger-ui/index.html" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan

# 测试 1: 用户登录
Write-Host "【1】测试登录接口" -ForegroundColor Yellow
Write-Host "POST $baseUrl/auth/login" -ForegroundColor Gray
try {
    $loginBody = @{
        email = "admin@example.com"
        password = "password"
    } | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "$baseUrl/auth/login" -Method POST -ContentType "application/json" -Body $loginBody
    
    if ($response.success) {
        $token = $response.data.accessToken
        Write-Host "✅ 登录成功！" -ForegroundColor Green
        Write-Host "   用户: $($response.data.user.username)" -ForegroundColor Gray
        Write-Host "   角色: $($response.data.user.roleName)" -ForegroundColor Gray
        Write-Host "   Token: $($token.Substring(0, 50))..." -ForegroundColor Gray
    } else {
        Write-Host "❌ 登录失败: $($response.message)" -ForegroundColor Red
        exit 1
    }
} catch {
    Write-Host "❌ 请求失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode)" -ForegroundColor Red
    if ($_.ErrorDetails) {
        Write-Host "   错误详情: $($_.ErrorDetails)" -ForegroundColor Red
    }
    exit 1
}

# 创建带 Token 的请求头
$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type" = "application/json"
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 2: 获取分类列表
Write-Host "【2】获取分类列表" -ForegroundColor Yellow
Write-Host "GET $baseUrl/categories" -ForegroundColor Gray
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/categories" -Method GET -Headers $headers
    if ($response.success) {
        Write-Host "✅ 成功！共 $($response.data.Count) 个分类" -ForegroundColor Green
        $response.data | Select-Object -First 3 | ForEach-Object {
            Write-Host "   - [$($_.id)] $($_.name)" -ForegroundColor Gray
        }
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 3: 获取分类树
Write-Host "【3】获取分类树" -ForegroundColor Yellow
Write-Host "GET $baseUrl/categories/tree" -ForegroundColor Gray
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/categories/tree" -Method GET -Headers $headers
    if ($response.success) {
        Write-Host "✅ 成功！" -ForegroundColor Green
        Write-Host "   树形结构: $($response.data | ConvertTo-Json -Depth 2 -Compress | Out-String | Select-Object -First 200)..." -ForegroundColor Gray
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 4: 获取文档列表（无分页）
Write-Host "【4】获取文档列表（无分页参数）" -ForegroundColor Yellow
Write-Host "GET $baseUrl/documents" -ForegroundColor Gray
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/documents" -Method GET -Headers $headers
    if ($response.success) {
        $count = if ($response.data.items) { $response.data.items.Count } elseif ($response.data) { $response.data.Count } else { 0 }
        Write-Host "✅ 成功！共 $count 个文档" -ForegroundColor Green
        if ($response.data.items) {
            $response.data.items | Select-Object -First 3 | ForEach-Object {
                Write-Host "   - [$($_.id)] $($_.title)" -ForegroundColor Gray
            }
        } elseif ($response.data) {
            $response.data | Select-Object -First 3 | ForEach-Object {
                Write-Host "   - [$($_.id)] $($_.title)" -ForegroundColor Gray
            }
        }
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 5: 获取文档列表（带分页）
Write-Host "【5】获取文档列表（带分页参数）" -ForegroundColor Yellow
Write-Host "GET $baseUrl/documents?page=1&size=10" -ForegroundColor Gray
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/documents?page=1&size=10" -Method GET -Headers $headers
    if ($response.success) {
        Write-Host "✅ 成功！" -ForegroundColor Green
        Write-Host "   总数: $($response.data.total)" -ForegroundColor Gray
        Write-Host "   当前页: $($response.data.page)" -ForegroundColor Gray
        Write-Host "   每页: $($response.data.size)" -ForegroundColor Gray
        Write-Host "   文档数: $($response.data.items.Count)" -ForegroundColor Gray
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 6: 获取用户列表
Write-Host "【6】获取用户列表" -ForegroundColor Yellow
Write-Host "GET $baseUrl/users" -ForegroundColor Gray
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/users" -Method GET -Headers $headers
    if ($response.success) {
        Write-Host "✅ 成功！共 $($response.data.Count) 个用户" -ForegroundColor Green
        $response.data | Select-Object -First 3 | ForEach-Object {
            Write-Host "   - $($_.username) ($($_.email))" -ForegroundColor Gray
        }
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 7: 获取角色列表
Write-Host "【7】获取角色列表" -ForegroundColor Yellow
Write-Host "GET $baseUrl/roles" -ForegroundColor Gray
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/roles" -Method GET -Headers $headers
    if ($response.success) {
        Write-Host "✅ 成功！共 $($response.data.Count) 个角色" -ForegroundColor Green
        $response.data | ForEach-Object {
            Write-Host "   - $($_.name): $($_.displayName)" -ForegroundColor Gray
        }
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
}

Write-Host "`n----------------------------------------`n" -ForegroundColor Cyan

# 测试 8: 创建文档
Write-Host "【8】创建测试文档" -ForegroundColor Yellow
Write-Host "POST $baseUrl/documents" -ForegroundColor Gray
try {
    $docBody = @{
        title = "API 测试文档 $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
        slug = "api-test-doc-$(Get-Date -Format 'yyyyMMddHHmmss')"
        summary = "这是通过 API 创建的测试文档"
        content = "# API 测试`n`n这是通过 PowerShell 脚本创建的测试文档。`n`n- 时间: $(Get-Date)`n- 用户: admin"
        contentType = "direct"
        categoryId = 1
        isPublic = $true
        tags = @("测试", "API")
    } | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "$baseUrl/documents" -Method POST -Headers $headers -Body $docBody
    if ($response.success) {
        Write-Host "✅ 创建成功！" -ForegroundColor Green
        Write-Host "   文档ID: $($response.data.id)" -ForegroundColor Gray
        Write-Host "   标题: $($response.data.title)" -ForegroundColor Gray
        Write-Host "   Slug: $($response.data.slug)" -ForegroundColor Gray
        
        # 保存文档ID以便后续测试
        $docId = $response.data.id
    }
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "   状态码: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
    if ($_.ErrorDetails) {
        Write-Host "   错误详情: $($_.ErrorDetails.Message)" -ForegroundColor Red
    }
}

Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "  测试完成！" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan

Write-Host "📝 查看 Swagger 文档:" -ForegroundColor Yellow
Write-Host "   http://localhost:8080/doc/swagger-ui/index.html`n" -ForegroundColor Cyan

